

package com.sms.day_report;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sms.DB.ConnectDB;
import com.sms.DB.OperateDB;
import com.sms.project.PReport;
import com.sms.user.Student;

public class Day_report {

	private int p_reportId;
	private String type;
	
	private int rootid;
	private int pid;
	private String isleaf;
	private String fromId;
	private String fromName;	
	private String toName;
	private int grade;		
	private String toId;
	private String p_reportTime;
	private String content;
	
	public void init(){}
//初始化日常汇报对象
public void init(Map map,int grade ){
	//if(map == null)System.out.println("null");
	//System.out.println((Integer)map.get("p_reportid"));
	this.setP_reportId((Integer)map.get("p_reportid"));
	this.setType(map.get("pr_type").toString());
	this.setRootid((Integer)map.get("rootid"));
	this.setPid((Integer)map.get("pid"));
	this.setIsleaf((String)map.get("isleaf"));
	this.setFromId((String)map.get("fromid"));
	this.setFromName((String)map.get("fromid"));
	this.setToId((String)map.get("toid"));
	this.setToName((String)map.get("toid"));
	this.setP_reportTime(map.get("p_reporttime").toString());
	this.setContent((String)map.get("content"));	
	this.setGrade(grade);
}	


//查询与删除项相关的所有汇报记录p_reportid

public ArrayList querydeid(int p_reportid){
	ArrayList<Integer> idlist = new ArrayList();
	List<Map> list = new ArrayList<Map>();	
	String sql = "select * from personreport where p_reportid = " + p_reportid;
	//System.out.println(sql);
	list = OperateDB.executeQuery(sql);
	if (list.size() > 0) {// 有项目时才初始化prrlist对象		
		//System.out.println("grade" + grade);		
			Map map = list.get(0);
			Day_report prr = new Day_report();
			prr.init(map, 0);
			int ids = (Integer)prr.getP_reportId();
			idlist.add(ids);
			if(prr.getIsleaf().equals("1")) {
				
				sql = "select * from personreport where pid = " + p_reportid;
				//System.out.println(sql);
				list = OperateDB.executeQuery(sql);
				if (list.size() > 0) {// 有项目时才初始化prrlist对象
				for(int i=0;i<list.size();i++){
					Map map1 = list.get(i);
					Day_report prr1 = new Day_report();
					prr.init(map1, 0);
					int ids0 = (Integer)prr.getP_reportId();
					idlist.add(ids0);					
					idlist.addAll(querydeid(ids0));	
				}	
				
				}
				
			}
		}
	return idlist;
}

//删除指定项的汇报
public boolean delete_report(int p_reportid){	
	ArrayList idlist = new ArrayList();
	boolean issuccess = false;
	idlist = querydeid(p_reportid);	
	System.out.println(idlist.size());
	for(int i=0; i<idlist.size();i++){
		int id = (Integer)idlist.get(i);
		String sql1="delete from personreport where p_reportid="+id+ " or pid="+id ;
		System.out.println(sql1);
		int n =OperateDB.update(sql1);
		if(n!=0){
			issuccess = true;
		}			
	}
	return issuccess;	
} 



//获取回复跟帖
public static List<Day_report> getReplyReport(Day_report pr, int grade) {
	List<Day_report> prrlist = new ArrayList<Day_report>();
	List<Map> list = new ArrayList<Map>();
	String sql = "";
	
	sql = "select * from personreport where pid = " + pr.getP_reportId();
	//System.out.println(sql);
	list = OperateDB.executeQuery(sql);
	if (list.size() > 0) {// 有项目时才初始化prrlist对象
		prrlist = new ArrayList<Day_report>();
		grade += 1;
		//System.out.println("grade" + grade);
		for (int i = 0; i < list.size(); i++) {
			Map map = list.get(i);
			Day_report prr = new Day_report();
			prr.init(map, grade);
			prrlist.add(prr);
			if(prr.getIsleaf().equals("1")) {
				prrlist.addAll(getReplyReport(prr, grade));
			} 
		}
	}
	return prrlist;
}

//添加汇报时插入数据
public static boolean inadddata(String limit,String fromid,String toid,String content){
	boolean a=true;
	// 向perreport表插入数据
			String	sql = "insert into personreport values(null, ?, 0, 0, '0', ?, ?,  now(), ?)";
			String forbid = "";
			if(limit.equals("all")) {
				forbid = "0";
			} 
			List<Object> tlist = new ArrayList<Object>();
			tlist.add(forbid);		
			tlist.add(fromid);
			tlist.add(toid);
			tlist.add(content);
			OperateDB.setList(tlist);
			int tn = OperateDB.getGeneratedKey(sql);

			//System.out.println(tn);

			if (tn == 0) { // 插入taskreport不成功
				a = false;
			} 			
	
	return a ;
}

//返回所有人员学号、姓名信息

public static List<Map> getallpeople(){
	String sql="select * from allpeople ";
	
	return OperateDB.executeQuery(sql);	
}


//返回老师 账号 姓名信息

public static List<Map> getallteacher(){
	String sql="select tno,tname from teacherinfo ";
	
	return OperateDB.executeQuery(sql);	
}





//获得指定用户的汇报(跟帖)
	public static List<Day_report> getPReport(String toID) {
		String sql = "select * from personreport where ((toid = '" + toID + "' ) or ( fromid = '" + toID + "'))" + 
						" and rootid = 0 and pid = 0";
		//System.out.println(sql);
		List<Day_report> prlist = new ArrayList<Day_report>();
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.executeQuery(sql);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map map = list.get(i);
				Day_report pr = new Day_report();
				pr.init(map,0);
				prlist.add(pr);
			}
		}
		
		return prlist;
	}

	//获得其他可见汇报(跟帖)
		public static List<Day_report> getOtherPReport(String toID) {
			String sql = "select * from personreport where  toid != '" + toID + "'" +
						" and  fromid != '" + toID + "' " + 
						" and pid = 0 " +
						" and pr_type = '0'" +
						" order by p_reporttime desc ";
			//System.out.println(sql);
			List<Day_report> prlist = new ArrayList<Day_report>();
			List<Map> list = new ArrayList<Map>();
			list = OperateDB.executeQuery(sql);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Map map = list.get(i);
					Day_report pr = new Day_report();
					pr.init(map,0);
					prlist.add(pr);
				}
			}
			
			return prlist;
		}
		
		//获得发给指定条数的汇报的(跟帖)
		public static List<Day_report> getOtherPReport( String toID, int startPos, int pageSize) {
			String sql = "select * from personreport where  toid != '" + toID + "'  " +
					" and  fromid != '" + toID + "' " + 
					" and pid = 0" + 
					" and pr_type = '0'" +
					" order by p_reporttime desc limit " + startPos + ", " + pageSize;
			//System.out.println(sql);
			List<Day_report> prlist = new ArrayList<Day_report>();
			List<Map> list = new ArrayList<Map>();

			list = OperateDB.executeQuery(sql);
			if (list.size() > 0) {// 有项目时才初始化prlist对象
				for (int i = 0; i < list.size(); i++) {
					Map map = list.get(i);
					Day_report pr = new Day_report();
					pr.init(map,0);
					prlist.add(pr);
				}
			}
			return prlist;
		}
		
		//获得指定项目发给指定条数的汇报的(跟帖)
		public static List<Day_report> getPReport( String toID, int startPos, int pageSize) {
			String sql = "select * from personreport where ((toid = '" + toID + "' ) or ( fromid = '" + toID + "'))" + 
					" and rootid = 0 " +
					" order by p_reporttime desc limit " + startPos + ", " + pageSize;
			//System.out.println(sql);
			List<Day_report> prlist = new ArrayList<Day_report>();
			List<Map> list = new ArrayList<Map>();

			list = OperateDB.executeQuery(sql);
			if (list.size() > 0) {// 有汇报时才初始化prlist对象
				for (int i = 0; i < list.size(); i++) {
					Map map = list.get(i);
					Day_report pr = new Day_report();
					pr.init(map,0);
					prlist.add(pr);
				}
			}
			return prlist;
		}

		
//回复帖子
		public boolean insertData(String type,int rootID, int pid, String fromID, String toID, String content) {
			boolean isSuccess = true;
			
			String sql = "";
			Connection conn = ConnectDB.getConnection();
			boolean autoCommit;
			try {
				autoCommit = conn.getAutoCommit();
				conn.setAutoCommit(false);// 取消自动提交
				
				sql =  "insert personreport values(null, ?, ?, ?, '0', ?, ?,  now(), ?)";
				List<Object> tlist = new ArrayList<Object>();
				tlist.add(type);
				tlist.add(1);
				tlist.add(pid);
				tlist.add(fromID);
				tlist.add(toID);
				tlist.add(content);
				OperateDB.setList(tlist);
				int id = OperateDB.getGeneratedKey(sql);
				if(id>=0) {
					
					String sql1 = "update personreport set isleaf = '0'where p_reportid =" + id ;
					String sql2 = "update personreport set isleaf = '1' where p_reportid = " + pid;
					if (OperateDB.update(sql1) > 0 && OperateDB.update(sql2) > 0) {
						isSuccess = true;
					} else {
						isSuccess = false;
					}
				} else {
					isSuccess = false;
				}
				
				if (isSuccess) {
					conn.commit();// 手动提交
				} else {
					conn.rollback();
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
				isSuccess = false;
			} finally {
				OperateDB.close(conn);
			}

			return isSuccess;
		}
		
	
//获取汇报学生的名字
	public  String getSname(String fromid) {
		String sql = "select * from stuinfo where sno = " + fromid;
		String name = "";
		//String type="";
		List<Map> list = new ArrayList<Map>();
		
		list = OperateDB.executeQuery(sql);
		if(list.size() > 0){
			Map map = list.get(0);			
			name = (String)map.get("sname");//获取汇报学生的名字
			//type=(String)map.get("stu_type");
		}
		return name ;			
	}
	
	//获取汇报学生的类型	
	public  String getStype(String fromid) {
		String sql = "select * from stuinfo where sno = " + fromid;
		//String name = "";
		String type="";
		List<Map> list = new ArrayList<Map>();
		
		list = OperateDB.executeQuery(sql);
		if(list.size() > 0){
			Map map = list.get(0);			
			//name = (String)map.get("sname");//获取汇报学生的名字
			type=(String)map.get("stu_type");
		}
		return type;
	}
//查询公开日常汇报
public static List<Map> queryall(){
	String sql="select p_reportid,pr_type,rootid,pid,fromid,toid,content,isleaf,p_reporttime,b.name as fromname,c.name as toname from personreport a,allpeople b,allpeople c where a.fromid=b.mno and a.toid=c.mno and pr_type='0'";
	
	return OperateDB.executeQuery(sql);
	
}
//查询权限日常汇报
public static List<Map> querylimit(){
	String sql="select * from personreport a,stuinfo b ,teacherinfo c where a.fromid=b.sno and a.toid=c.tno ";	
	return OperateDB.executeQuery(sql);
	
}


//向上汇报，并插入数据库

public static String insert(String sql){
	if(OperateDB.insert(sql)){
		return "插入成功！";	
	}		
	return "汇报失败！";		
}


	
	
	public int getP_reportId() {
		return p_reportId;
	}
	public void setP_reportId(int p_reportId) {
		this.p_reportId = p_reportId;
	}
	public String getFromId() {
		return fromId;
	}
	public void setFromId(String fromId) {
		this.fromId = fromId;
	}
	public String getToId() {
		return toId;
	}
	public void setToId(String toId) {
		this.toId = toId;
	}
	public String getP_reportTime() {
		return p_reportTime;
	}
	public void setP_reportTime(String p_reportTime) {
		this.p_reportTime = p_reportTime;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getRootid() {
		return rootid;
	}

	public void setRootid(int rootid) {
		this.rootid = rootid;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getIsleaf() {
		return isleaf;
	}

	public void setIsleaf(String isleaf) {
		this.isleaf = isleaf;
	}
	
	public String getFromName() {
		return fromName;
	}

	public void setFromName(String fromID) {
		String sql = "select * from teacherinfo where tno = '" + fromID + "' ";
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.executeQuery(sql);
		if (list != null && list.size() != 0) {
			Map map = list.get(0);
			this.fromName = (String) map.get("tname");
			fromName += "[老师]";
		} else {
			sql = "select * from stuinfo where sno = '" + fromID + "' ";
			list = OperateDB.executeQuery(sql);
			if (list != null && list.size() != 0) {
				Map map = list.get(0);
				this.fromName = (String) map.get("sname");
			}
		}

	}

	public String getToName() {
		return toName;
	}

	public void setToName(String toId) {
		String sql = "select * from teacherinfo where tno = '" + toId + "' ";
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.executeQuery(sql);
		if (list != null && list.size() != 0) {
			Map map = list.get(0);
			this.toName = (String) map.get("tname");
			toName += "[老师]";
		} else {
			sql = "select * from stuinfo where sno = '" + toId + "' ";
			list = OperateDB.executeQuery(sql);
			if (list != null && list.size() != 0) {
				Map map = list.get(0);
				this.toName = (String) map.get("sname");
			}
		}
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}


}
